<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"
          v-if="hasPermission('ps:ps_working_calendar:add')">
          新增</a-button>
        <!-- <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"
          v-if="hasPermission('ps:ps_working_calendar:exportXls')">
          导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button> -->
        <super-query :config="superQueryConfig" @search="handleSuperQuery" />
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small"
          @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <PsWorkingCalendarModal @register="registerModal" @success="handleSuccess"></PsWorkingCalendarModal>
    <BusCancelWorkOrder @register="registerBusCancelWorkOrder" @success="handleSuccess"> </BusCancelWorkOrder>
  </div>
</template>

<script lang="ts" name="psWorkingCalendar-psWorkingCalendar" setup>
import { reactive, toRaw } from 'vue';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { useModal } from '/@/components/Modal';
import PsWorkingCalendarModal from './components/PsWorkingCalendarModal.vue';
import { columns, searchFormSchema } from './PsWorkingCalendar.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, generateCalendar } from './PsWorkingCalendar.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import { usePermission } from '/@/hooks/web/usePermission';
import BusCancelWorkOrder from './components/BusCancelWorkOrder.vue'

const { hasPermission } = usePermission();
//注册model
const [registerModal, { openModal }] = useModal();
const [registerBusCancelWorkOrder, { openModal: openBusCancelWorkOrder }] = useModal({});
//注册table数据
const { tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '工作日历',
    api: list,
    columns,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    actionColumn: {
      width: 240,
      fixed: 'right',
    },
  },
  exportConfig: {
    name: '工作日历',
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess,
  },
});

const [registerTable, { reload, setProps }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 新增事件
 */
function handleAdd() {
  openModal(true, {
    isUpdate: false,
    showFooter: true,
    title: '新增'
  });
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
    title: '编辑'
  });
}
/**
 * 详情
 */
function handleDetail(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
    title: '详情'
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
//生成日历
async function handleGenerateCalendar(record) {
  await generateCalendar({ id: record.id }, handleSuccess);
}
/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'ps:ps_working_calendar:edit', //通过权限指令控制显示（有权限显示/无权限不显示）
      ifShow: record.isGenerate === 0 ? true : false
    },
    {
      label: '生成日历',
      popConfirm: {
        title: '是否确认生成日历',
        confirm: handleGenerateCalendar.bind(null, record),
      },
      auth: 'ps:ps_working_calendar:generateCalendar', //通过权限指令控制显示（有权限显示/无权限不显示）
      ifShow: record.isGenerate === 0 ? true : false
    },    {
      label: '追加日历',
      onClick: unCancelWorkOrderItem.bind(null, record),
    },
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
      // auth: '', //通过权限指令控制显示（有权限显示/无权限不显示）
      ifShow: record.isGenerate === 1 ? true : false
    },
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        placement: 'left',
        confirm: handleDelete.bind(null, record),
      },
      auth: 'ps:ps_working_calendar:delete', //通过权限指令控制显示（有权限显示/无权限不显示）
    }
  ];
}
function unCancelWorkOrderItem(record: Recordable) {
  openBusCancelWorkOrder(true, {
    record,
    isUpdate: false,
    showFooter: true,
  });
}
// 高级查询配置
const superQueryConfig = reactive({
  resourceGroupNumber: { title: '工作日历编码', view: 'text', type: 'string', order: 1 },
  groupName: { title: '工作日历名称', view: 'text', type: 'string', order: 1 },
  whethereffective: { title: '开始时间', view: 'date', type: 'string', order: 1 },
  groupingDescribe: { title: '结束时间', view: 'date', type: 'string', order: 1 },
  isGenerate: {
    title: '是否已生成',
    view: 'list',
    type: 'string',
    enum: [
      { value: 1, title: '是' },
      { value: 0, title: '否' },
    ],
    order: 1,
  },
  remark: { title: '备注', view: 'text', type: 'string', order: 1 },
});
//执行查询
function handleSuperQuery(params) {
  setProps({ searchInfo: toRaw(params) });
  reload();
}
</script>

<style scoped></style>
